package com.iplant.basic.controller.bfc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.iplant.base.po.OutResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.ErrorLogItem;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.BFCService;
import com.iplant.basic.service.po.bms.BMSEmployee;

@RestController
@RequestMapping("/api/ErrorLog")
public class ErrorLogController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(ErrorLogController.class);

    @Autowired
    BFCService wBFCService;

    @GetMapping("/LogList")
    Object LogList(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<List<ErrorLogItem>> wServiceResult = wBFCService.BFC_QueryLogFileList(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            wResult = GetResult(RetCode.SERVER_CODE_SUC, RetCode.SERVER_CODE_ERR_MSG);
            logger.error(e.toString());
        }
        return wResult;
    }

    @GetMapping("/LogInfo")
    Object LogInfo(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            String wFilePath = StringUtils.parseString(request.getParameter("FilePath"));
            ServiceResult<List<String>> wServiceResult = wBFCService.BFC_SelectLogFile(wBMSEmployee, wFilePath);
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception e) {
            wResult = GetResult(RetCode.SERVER_CODE_SUC, RetCode.SERVER_CODE_ERR_MSG);
            logger.error(e.toString());
        }
        return wResult;
    }

    /**
     * post方式获取文件内容
     */
    @PostMapping("/LogInfoByPost")
    public Object LogInfoByPost(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            String wFilePath = StringUtils.parseString(wParam.get("FilePath"));

            ServiceResult<List<String>> wServiceResult = wBFCService.BFC_SelectLogFile(wLoginUser, wFilePath);
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/DeleteInfo")
    Object DeleteInfo(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            String wFilePath = StringUtils.parseString(request.getParameter("FilePath"));
            ServiceResult<Boolean> wServiceResult = wBFCService.BFC_DeleteLogFile(wBMSEmployee, wFilePath);
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }

        } catch (Exception e) {
            wResult = GetResult(RetCode.SERVER_CODE_SUC, RetCode.SERVER_CODE_ERR_MSG);
            logger.error(e.toString());
        }
        return wResult;
    }

    @PostMapping("/DeleteList")
    Object DeleteList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            List<String> wFilePathList = CloneTool.CloneArray(wParam.get("FilePath"), String.class);
            ServiceResult<String> wServiceResult = wBFCService.BFC_DeleteLogFileList(wBMSEmployee, wFilePathList);
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception e) {
            wResult = GetResult(RetCode.SERVER_CODE_SUC, RetCode.SERVER_CODE_ERR_MSG);
            logger.error(e.toString());
        }
        return wResult;
    }

    /**
     * 清空文件内容
     */
    @PostMapping("/EmptyFile")
    public Object EmptyFile(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            // 获取参数
            String wFilePath = StringUtils.parseString(wParam.get("FilePath"));
            if (StringUtils.isEmpty(wFilePath)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wBFCService.ELG_EmptyFile(wLoginUser, wFilePath);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
